<?php
/**
 * Created by PhpStorm.
 * User: yafex
 * Date: 2019/8/23
 * Time: 14:27
 */
class model_codeGenerator extends L{

    /**
     * 描述：查询数据库名称
     * 作者：Zoro.Zhu
    **/
    public static function getSchema(){
        return L::sql("SELECT
            TABLE_SCHEMA
        FROM
            `COLUMNS`
        GROUP BY
            TABLE_SCHEMA
        ORDER BY 
            TABLE_SCHEMA", 'info');
    }

    /**
     * 描述：查询数据库的表名
     * 作者：Zoro.Zhu
    **/
    public static function getFromName($schema){
        return L::sql("SELECT
            TABLE_NAME
        FROM
            `COLUMNS`
        WHERE
            TABLE_SCHEMA = '{$schema}'
        GROUP BY
            TABLE_NAME
        ORDER BY 
            TABLE_NAME", 'info');
    }

    /**
     * 描述：获取字段列的数据
     * 作者：Zoro.Zhu
    **/
    public static function getColumnList($schema, $fromName){
        $sql = "SELECT
            TABLE_SCHEMA,-- AS '库名',
            TABLE_NAME,-- AS '表名',
            COLUMN_NAME,-- AS '列名',
            ORDINAL_POSITION,-- AS '列的排列顺序',
            COLUMN_DEFAULT,-- AS '默认值',
            IS_NULLABLE,-- AS '是否为空',
            DATA_TYPE,-- AS '数据类型',
            COLUMN_COMMENT,-- AS '注释'
            COLUMN_KEY-- AS 'KEY'
        FROM
            `COLUMNS`
        WHERE
            TABLE_SCHEMA = '{$schema}'
        AND TABLE_NAME = '{$fromName}'
        ORDER BY
            TABLE_NAME,
            ORDINAL_POSITION";
        return L::sql($sql, 'info');
    }

    /**
     * 描述：获取表的注释
     * 作者：Zoro.Zhu
    **/
    public static function getTableComment($schema, $fromName){
        $sql = "SELECT
            TABLE_NAME, TABLE_COMMENT
        FROM
            TABLES 
        WHERE
            TABLE_SCHEMA = '{$schema}' 
        AND TABLE_NAME = '{$fromName}'";
        $res = L::sql($sql, 'info');
        return !empty($res[0]) ? $res[0]['TABLE_COMMENT'] : '';
    }

}